package com.example.controller;

import com.example.api.Result;
import com.example.configura.LogOperation;
import com.example.params.StudentQueryParam;
import com.example.pojo.Student;
import com.example.service.StudentSevice;
import com.example.vo.student.GetAllStudentInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/students")
public class StudentController {
    @Autowired
    private StudentSevice studentService;
    @GetMapping
    public Result<GetAllStudentInfoVo> queryAllStudent(StudentQueryParam studentQueryParam) {
        log.info("查询所有学生信息,请求参数为：{}",studentQueryParam);
        return Result.success(studentService.queryAllStudent(studentQueryParam));
    }
    @PostMapping
    @LogOperation
    public Result<String> addStudent(@RequestBody Student student) {
        log.info("添加学生信息,请求参数为：{}",student);
        return studentService.addStudent(student) ? Result.success(null) : Result.error(0,"添加失败");
    }

    @GetMapping("/{id}")
    public Result<Student> getStudentById(@PathVariable Integer id) {
        log.info("查询学生信息,请求参数为：{}",id);
        return Result.success(studentService.getStudentById(id));
    }
    @PutMapping
    @LogOperation
    public Result<String> updateStudent(@RequestBody Student student) {
        log.info("修改学生信息,请求参数为：{}",student);
        return studentService.updateStudent(student) ? Result.success(null) : Result.error(0,"修改失败");
    }
    @DeleteMapping("/{ids}")
    @LogOperation
    public Result<String> deleteStudentByIds(@PathVariable("ids") List<Integer> ids) {
        log.info("删除学生信息,请求参数为：{}",ids);
        return studentService.deleteStudentByIds(ids) ? Result.success(null) : Result.error(0,"删除失败");
    }
    @PutMapping("/violation/{id}/{score}")
    @LogOperation
    public Result<String> handleViolation(@PathVariable Integer id, @PathVariable Integer score) {
        log.info("处理学生信息,请求参数为：{}",id);
        return studentService.handleViolation(id,score) ? Result.success(null) : Result.error(0,"处理失败");
    }
}
